﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using cfg = System.Configuration;
using System.Collections;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_CommerCheckOutPutContract : DockContent
    {
        private DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();
        private bool m_bHaveChange = false;
        private string m_strFolder = "d:/DGExcel/";
        Hashtable m_SQLStringList = new Hashtable();
        ArrayList m_arrayList = new ArrayList();
        private bool m_bFlag = false;
        private int m_RowIndex = 0;
        private DGBY.BLL.LogRecTab m_bllLogRecTab = new DGBY.BLL.LogRecTab();
        private DGBY.BLL.SJGouHHTTab m_bllSJGouHHTTab = new DGBY.BLL.SJGouHHTTab();
        private DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        //private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private DGBY.BLL.NumDanWeiTab m_bllNumDanWeiTab = new DGBY.BLL.NumDanWeiTab();
        private DGBY.BLL.SJPackGoodsMXTab m_bllSJPackGoodsMXTab = new DGBY.BLL.SJPackGoodsMXTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.SJPackingTab m_bllSJPackingTab = new DGBY.BLL.SJPackingTab();
        private DGBY.BLL.T_ACL_User m_bllT_ACL_User = new DGBY.BLL.T_ACL_User();
        private DGBY.BLL.QDPlaceTab m_bllQDPlaceTab = new DGBY.BLL.QDPlaceTab();
        private DGBY.BLL.YSMethodTab m_bllYSMethodTab = new DGBY.BLL.YSMethodTab();
        private DGBY.BLL.MDCountryTab m_bllMDCountryTab = new DGBY.BLL.MDCountryTab();
        private DGBY.BLL.SJCKHTTab m_bllSJCKHTTab = new DGBY.BLL.SJCKHTTab();
        private DGBY.BLL.BGPackingTab m_bllBGPackingTab = new DGBY.BLL.BGPackingTab();
        private DGBY.BLL.SHCompTab m_bllSHCompTab = new DGBY.BLL.SHCompTab();
        private DGBY.BLL.BYCompanyTab bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.BGPackGoodsMXTab m_bllBGPackGoodsMXTab = new DGBY.BLL.BGPackGoodsMXTab();
        private DGBY.BLL.ZYHarborTab m_bllZYHarborTab = new DGBY.BLL.ZYHarborTab();
        private DGBY.BLL.FeeSJTruePayTab m_bllFeeSJTruePayTab = new DGBY.BLL.FeeSJTruePayTab();
       // private DGBY.BLL.SJPackGoodsMXTab m_bllSJPackGoodsMXTab = new DGBY.BLL.SJPackGoodsMXTab();
        private DataSet m_ds = new DataSet();
        private DataSet m_dsPaidan = new DataSet();
        
        private int m_nSelectSJCKHTTabID = 0;

        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        //ExcelBase eB = new ExcelBase();
        private int m_nSelectSJPackGoodsMXTabID = 0;
        //public static int m_nPaidanRegisterTabID = 0;

        public Frm_CommerCheckOutPutContract()
        {
            InitializeComponent();
        }
        private bool CheckSBDate()
        {
            bool bRet = false;
            if (!Frm_Main.m_bCompleteVer)
            {
                DataSet dsHxdNoTab = m_bllHxdNoTab.GetList("HxdNoTab.PaidanRegisterTabID = '" + this.comboBoxContractNo.SelectedValue + "'");
                if (dsHxdNoTab.Tables.Count > 0 && dsHxdNoTab.Tables[0].Rows.Count > 0)
                {
                    int nHxdNoTabID = Convert.ToInt32(dsHxdNoTab.Tables[0].Rows[0]["HxdNoTabID"].ToString(), 10);
                    DGBY.Model.HxdNoTab modelHxdNoTabOld = m_bllHxdNoTab.GetModel(nHxdNoTabID);
                    int nYear = modelHxdNoTabOld.SBDate.Value.Date.Year;
                    if (nYear != 1900)
                    {
                        bRet = true;
                    }
                }

            }

            return bRet;
        }
        //public Frm_CommerCheckOutPutContract(int nID)
        //{
        //    InitializeComponent();
        //    m_nPaidanRegisterTabID = nID;
        //}

        private void InitCombox()
        {
            m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndID("");
            if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                //if (m_nPaidanRegisterTabID != 0)
                //    this.comboBoxContractNo.SelectedValue = m_nPaidanRegisterTabID;
                //else
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            }

            DataSet ds = m_bllT_ACL_User.GetList("Name != 'System1'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxSellPerson.DataSource = ds.Tables[0];
                this.comboBoxSellPerson.DisplayMember = "用户名称";
                this.comboBoxSellPerson.ValueMember = "UserPasswordTabID";
                this.comboBoxSellPerson.SelectedIndex = 0;
            }

            ds = m_bllQDPlaceTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxMakeProtocolPlace.DataSource = ds.Tables[0];
                this.comboBoxMakeProtocolPlace.DisplayMember = "签订地点";
                this.comboBoxMakeProtocolPlace.ValueMember = "QDPlaceTabID";
                this.comboBoxMakeProtocolPlace.SelectedIndex = 0;
            }

            ds = m_bllYSMethodTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxTransWay.DataSource = ds.Tables[0];
                this.comboBoxTransWay.DisplayMember = "运输方式";
                this.comboBoxTransWay.ValueMember = "YSMethodTabID";
                this.comboBoxTransWay.SelectedIndex = 0;
            }

            ds = m_bllMDCountryTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxTargetCountry.DataSource = ds.Tables[0];
                this.comboBoxTargetCountry.DisplayMember = "国家名称";
                this.comboBoxTargetCountry.ValueMember = "MDCountryTabID";
                this.comboBoxTargetCountry.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxTargetCountry.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxTargetCountry.SelectedIndex = 0;
            }          
        }
        private void InitContractNoCombox()
        {
            try
            {
                if (Frm_Main.m_nContractSelectValue == 127)
                    return;
                DataRow[] drs = m_dsPaidan.Tables[0].Select("PaidanRegisterTabID = '" + Frm_Main.m_nContractSelectValue.ToString() + "'");
                if (drs.Length > 0)
                    this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                else
                {
                    m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndIDAll("");
                    if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
                    {
                        this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                        this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                        this.comboBoxContractNo.DisplayMember = "合同编号";
                        this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                        this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                        this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                    }

                    
                    this.comboBoxSellPerson.SelectedIndex = 0;
           

                    this.comboBoxMakeProtocolPlace.SelectedIndex = 0;
         
                    this.comboBoxTransWay.SelectedIndex = 0;
                    this.comboBoxTargetCountry.SelectedIndex = 0;
                      
                }
            }
            catch (System.Exception ex)
            {
                //  MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            //DataSet ds = m_bllPaidanRegisterTab.GetAllPaiDanList("");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.comboBoxContractNo.DataSource = ds.Tables[0];
            //    this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
            //    this.comboBoxContractNo.DisplayMember = "合同编号";
            //    this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
            //    this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //    //if (m_nPaidanRegisterTabID != 0)
            //    //    this.comboBoxContractNo.SelectedValue = m_nPaidanRegisterTabID;
            //    //else
            //    this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
            //}
        }
        private void Frm_CommerCheckOutPutContract_Load(object sender, EventArgs e)
        {
            InitCombox();
            InitContractNoCombox();

            this.button1.Visible = Frm_Main.m_bCompleteVer;
            this.checkBoxDisCountSJ.Visible = Frm_Main.m_bCompleteVer;

        }

        private void ShowContractSheetByContractCode(string strContractCode)
        {
            try
            {
                DataSet ds = m_bllSJPackingTab.ShowOneSJPackRec("PaidanRegisterTab.ContractCode = '" + strContractCode + "'");
                int nPackingId = 0;
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    nPackingId = Convert.ToInt32(ds.Tables[0].Rows[0]["SJPackingTabID"].ToString(), 10);
                }
                ds = m_bllSJCKHTTab.ShowOneOutContractRec("ContractCode = '" + strContractCode + "'", nPackingId);
                ultraGrid1.DataSource = null;

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    m_nSelectSJCKHTTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["SJCKHTTabID"].ToString());
                    cellSJCKHTTabClick();
                    ultraGrid1.DataSource = ds.Tables[0];
                    ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                    ShowAllMXRecordInDataGridView(strContractCode);
                }
                else
                {
                    ultraGrid2.DataSource = null;
                    m_nSelectSJCKHTTabID = 0;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }
        private void SyncContractNo()
        {
            try
            {
                //string str = this.comboBoxContractNo.SelectedValue.ToString();
               

                int nID = Frm_Main.m_nContractSelectValue;
                //m_nPaidanRegisterTabID = nID;
                DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTabID = '" + nID + "'");
                string strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
                int nUserPasswordTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["UserPasswordTabID"].ToString(), 10);
                DataSet ds2 = m_bllSJPackingTab.ShowOneSJPackRec("ContractCode = '" + strContractCode + "'");
                if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                {
                    this.textBoxSellSide.Text = ds2.Tables[0].Rows[0]["经营单位"].ToString();
                    this.textBoxBuySide.Text = ds2.Tables[0].Rows[0]["收货单位"].ToString();
                    this.textBoxPort.Text = ds2.Tables[0].Rows[0]["装运港"].ToString();
                    this.textBoxOutSideCate.Text = ds2.Tables[0].Rows[0]["包装种类"].ToString();
                    this.comboBoxSellPerson.SelectedValue = nUserPasswordTabID;
                    //this.textBox1ContractNoGoodsDetail.Text = strContractCode;


                }
                ShowContractSheetByContractCode(strContractCode);

            }
            catch { }
        }

        private void comboBoxContractNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxContractNo.SelectedValue.ToString();

                
                int nID = Convert.ToInt32(str, 10);
                //m_nPaidanRegisterTabID = nID;
                DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTabID = '" + nID + "'");
                string strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
                int nUserPasswordTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["UserPasswordTabID"].ToString(), 10);
                DataSet ds2 = m_bllSJPackingTab.ShowOneSJPackRec("ContractCode = '" + strContractCode + "'");
                if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                {
                    this.textBoxSellSide.Text = ds2.Tables[0].Rows[0]["经营单位"].ToString();
                    this.textBoxBuySide.Text = ds2.Tables[0].Rows[0]["收货单位"].ToString();
                    this.textBoxPort.Text = ds2.Tables[0].Rows[0]["装运港"].ToString();
                    this.textBoxOutSideCate.Text = ds2.Tables[0].Rows[0]["包装种类"].ToString();
                    this.comboBoxSellPerson.SelectedValue = nUserPasswordTabID;
                    //this.textBox1ContractNoGoodsDetail.Text = strContractCode;


                }
                else
                {
                    this.textBoxSJGuiCount.Text = "";
                }

                ShowContractSheetByContractCode(strContractCode);
                
            }
            catch { }
        }

        private void ShowAllRecordInDataGridView(string strWhere)
        {

            m_ds = m_bllSJCKHTTab.ShowOneOutContractRecEx(strWhere);
            ultraGrid1.DataSource = null;

            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            m_dsPaidan = m_bllPaidanRegisterTab.GetNameAndIDAll("");
            if (m_dsPaidan.Tables.Count > 0 && m_dsPaidan.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = m_dsPaidan.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                //if (m_nPaidanRegisterTabID != 0)
                this.comboBoxContractNo.SelectedValue = Frm_Main.m_nContractSelectValue;
                //else
                // this.comboBoxContractNo.SelectedIndex = Frm_Main.m_nContractSelectIndex;
            }
            ShowAllRecordInDataGridView("");
            this.ultraGrid1.Dock = DockStyle.Fill;
            this.groupBoxGoodsDetail.Visible = false;
        }

        //private void dataGridViewOutSideContractList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        //{
        //    string strSJCKHTTab = this.dataGridViewOutSideContractList.Rows[this.dataGridViewOutSideContractList.CurrentCell.RowIndex].Cells[0].Value.ToString();
        //    m_nSelectSJCKHTTabID = Convert.ToInt32(strSJCKHTTab, 10);
        //    cellSJCKHTTabClick();
        //}

        private void cellSJCKHTTabClick()
        {
            this.ultraGrid1.Dock = DockStyle.None;
            this.groupBoxGoodsDetail.Visible = true;
            DGBY.Model.SJCKHTTab modelSJCKHTTab = m_bllSJCKHTTab.GetModel(m_nSelectSJCKHTTabID);
            if (modelSJCKHTTab != null)
            {
                this.comboBoxSellPerson.Text = modelSJCKHTTab.JYCompDaiBiao;
                this.textBoxBuyPerson.Text = modelSJCKHTTab.SHCompDaiBiao;
                this.textBoxDYZ.Text = modelSJCKHTTab.DYZ;
                this.textBoxDanBao.Text = modelSJCKHTTab.BaoXian;
                this.textBoxPayCondition.Text = modelSJCKHTTab.FKCondition;
                this.textBoxGoodsCheck.Text = modelSJCKHTTab.SPCheck;
                this.textBoxProblemSolve.Text = modelSJCKHTTab.ZYMethod;
                this.textBoxProductNo.Text = modelSJCKHTTab.SCPiNo;
                this.textBoxOther.Text = modelSJCKHTTab.QTThing;
                this.textBoxNote.Text = modelSJCKHTTab.Note;
                this.textBoxSJGuiCount.Text = modelSJCKHTTab.SJGuiCount.ToString();
                //this.textBoxSecondSheetNo.Text = modelSJCKHTTab.SJCKHTDJNo.ToString();
                this.textBoxInvoiceNo.Text = modelSJCKHTTab.InvoiceNo;
                this.dateTimePickerMakeProtocolDate.Value = (System.DateTime)modelSJCKHTTab.QDDate;
                this.dateTimePickerDeadLine.Value = (System.DateTime)modelSJCKHTTab.JHDate;
                this.checkBoxDisCountSJ.Checked = modelSJCKHTTab.isDisCount;
                
                if (modelSJCKHTTab.QDPlaceTabID != null)
                    this.comboBoxMakeProtocolPlace.SelectedValue = modelSJCKHTTab.QDPlaceTabID;
                if (modelSJCKHTTab.YSMethodTabID != null)
                    this.comboBoxTransWay.SelectedValue = modelSJCKHTTab.YSMethodTabID;
                if (modelSJCKHTTab.MDCountryTabID != null)
                    this.comboBoxTargetCountry.SelectedValue = modelSJCKHTTab.MDCountryTabID;

                if (modelSJCKHTTab.PaidanRegisterTabID != null)
                    this.comboBoxContractNo.SelectedValue = modelSJCKHTTab.PaidanRegisterTabID;


                //ShowAllMXRecordInDataGridView(m_nSelectSJPackingTabID);

            }
        }

        //private void dataGridViewOutSideContractList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        //{
        //    {
        //        using (SolidBrush b = new SolidBrush(dataGridViewOutSideContractList.RowHeadersDefaultCellStyle.ForeColor))
        //        {
        //            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
        //        }
        //    }
        //}

        private Int64 MakeSJCKouDJNo()
        {
            string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');
            DataSet ds = m_bllSJCKHTTab.GetSJPackDJNoByDate("SJCKHTDJDate = '" + strDate + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MaxJPackDJNum"].ToString();
                long nDJNo = 0;
                if (strDJNo != "")
                    nDJNo = Convert.ToInt64(strDJNo, 10);
                nDJNo++;
                string strTemp = "";
                if (strDJNo != "")
                    strTemp = string.Format("{0:d4}", nDJNo);
                else
                    strTemp = strDate + string.Format("{0:d4}", nDJNo);

                return Convert.ToInt64(strTemp, 10);
            }

            return 0;
        }

        private void buttonAddFillBoxSheet_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    DataSet ds = m_bllSJPackingTab.ShowOneSJPackRec("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    {

            //    }
            //    else
            //    {
            //        MessageBox.Show("该合同编号没有对应的商检_装箱单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }

            //    ds = m_bllSJCKHTTab.ShowOneOutContractRec("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    {
            //        MessageBox.Show("该合同编号已有对应的商检_出口合同!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }
            //    DGBY.Model.SJCKHTTab modelSJCKHTTab = new DGBY.Model.SJCKHTTab();

            //    modelSJCKHTTab.JYCompDaiBiao = this.comboBoxSellPerson.Text;
            //    modelSJCKHTTab.SHCompDaiBiao = this.textBoxBuyPerson.Text;
            //    modelSJCKHTTab.DYZ = this.textBoxDYZ.Text;
            //    modelSJCKHTTab.BaoXian = this.textBoxDanBao.Text;
            //    modelSJCKHTTab.FKCondition = this.textBoxPayCondition.Text;
            //    modelSJCKHTTab.SPCheck = this.textBoxGoodsCheck.Text;
            //    modelSJCKHTTab.ZYMethod = this.textBoxProblemSolve.Text;
            //    modelSJCKHTTab.SCPiNo = this.textBoxProductNo.Text;
            //    modelSJCKHTTab.QTThing = this.textBoxOther.Text;
            //    modelSJCKHTTab.Note = this.textBoxNote.Text;

            //    modelSJCKHTTab.SJCKHTDJNo = MakeSJCKouDJNo();

            //    modelSJCKHTTab.QDDate = this.dateTimePickerMakeProtocolDate.Value.Date;
            //    modelSJCKHTTab.JHDate = this.dateTimePickerMakeProtocolDate.Value.AddMonths(1).Date; 
                
            //    modelSJCKHTTab.SJCKHTDJDate = System.DateTime.Now.Date;
            //    modelSJCKHTTab.InvoiceNo = this.textBoxInvoiceNo.Text;
            //    modelSJCKHTTab.InvoiceNote = "";

            //    if (this.comboBoxMakeProtocolPlace.SelectedValue != null)
            //        modelSJCKHTTab.QDPlaceTabID = Convert.ToInt32(this.comboBoxMakeProtocolPlace.SelectedValue.ToString(), 10);
            //    if (this.comboBoxTransWay.SelectedValue != null)
            //        modelSJCKHTTab.YSMethodTabID = Convert.ToInt32(this.comboBoxTransWay.SelectedValue.ToString(), 10);
            //    if (this.comboBoxTargetCountry.SelectedValue != null)
            //        modelSJCKHTTab.MDCountryTabID = Convert.ToInt32(this.comboBoxTargetCountry.SelectedValue.ToString(), 10);
            //    if (this.comboBoxContractNo.SelectedValue != null)
            //        modelSJCKHTTab.PaidanRegisterTabID = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);


            //    m_bllSJCKHTTab.Add(modelSJCKHTTab);
            //    ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTabID = '" + modelSJCKHTTab.PaidanRegisterTabID + "'");
            //    string strContractCode = ds.Tables[0].Rows[0]["ContractCode"].ToString();
            //    //m_bllSJCKHTTab.InitCostField(strContractCode);
            //    ShowAllRecordInDataGridView("PaidanRegisterTab.PaidanRegisterTabID = '" + modelSJCKHTTab.PaidanRegisterTabID + "'");
            //    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //}
         
        }

        private void buttonModifyFillBoxSheet_Click(object sender, EventArgs e)
        {
            if (CheckSBDate())
            {
                MessageBox.Show("综合登记表该合同号已经登记申报日期，不能修改单据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                m_SQLStringList.Clear();
                m_arrayList.Clear();
                DGBY.Model.SJCKHTTab modelSJCKHTTabOld = m_bllSJCKHTTab.GetModel(m_nSelectSJCKHTTabID);
                if (modelSJCKHTTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                int nLimitGuiCount = m_bllSJPackingTab.GetOneSJPackRecCountByPaidanID(modelSJCKHTTabOld.PaidanRegisterTabID);
                int nModifyGuiCount = Convert.ToInt32(this.textBoxSJGuiCount.Text, 10);
                bool bModifyShouldRecvMoney = false;

                if (1 != nLimitGuiCount)
                {
                    if (nModifyGuiCount < nLimitGuiCount)
                    {
                        MessageBox.Show("商检柜数必须大于或等于商检装箱单的记录数量!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }

                //DGBY.Model.SJCKHTTab modelSJCKHTTab = new DGBY.Model.SJCKHTTab();
                //modelSJCKHTTab.SJCKHTTabID = m_nSelectSJCKHTTabID;
                modelSJCKHTTabOld.SJCKHTDJNo = modelSJCKHTTabOld.SJCKHTDJNo;

                modelSJCKHTTabOld.JYCompDaiBiao = this.comboBoxSellPerson.Text;
                modelSJCKHTTabOld.SHCompDaiBiao = this.textBoxBuyPerson.Text;
                modelSJCKHTTabOld.DYZ = this.textBoxDYZ.Text;
                modelSJCKHTTabOld.BaoXian = this.textBoxDanBao.Text;
                modelSJCKHTTabOld.FKCondition = this.textBoxPayCondition.Text;
                modelSJCKHTTabOld.SPCheck = this.textBoxGoodsCheck.Text;
                modelSJCKHTTabOld.ZYMethod = this.textBoxProblemSolve.Text;
                modelSJCKHTTabOld.SCPiNo = this.textBoxProductNo.Text;
                modelSJCKHTTabOld.QTThing = this.textBoxOther.Text;
                modelSJCKHTTabOld.Note = this.textBoxNote.Text;

                if (modelSJCKHTTabOld.SJGuiCount == 0 && nModifyGuiCount != 0)
                {
                    bModifyShouldRecvMoney = true;
                }

                modelSJCKHTTabOld.SJGuiCount = nModifyGuiCount;
                

                if (modelSJCKHTTabOld.isDisCount != this.checkBoxDisCountSJ.Checked)
                {

                    ModifyContractDetail();

                }


                modelSJCKHTTabOld.isDisCount = this.checkBoxDisCountSJ.Checked;

                //modelSJCKHTTabOld.QDDate = this.dateTimePickerMakeProtocolDate.Value.Date;
                //modelSJCKHTTabOld.JHDate = this.dateTimePickerMakeProtocolDate.Value.AddMonths(1).Date;

                //modelSJCKHTTabOld.SJCKHTDJDate = modelSJCKHTTabOld.SJCKHTDJDate;
                modelSJCKHTTabOld.InvoiceNo = this.textBoxInvoiceNo.Text;
                modelSJCKHTTabOld.InvoiceNote = "";

                if (this.comboBoxMakeProtocolPlace.SelectedValue != null)
                    modelSJCKHTTabOld.QDPlaceTabID = Convert.ToInt32(this.comboBoxMakeProtocolPlace.SelectedValue.ToString(), 10);
                if (this.comboBoxTransWay.SelectedValue != null)
                    modelSJCKHTTabOld.YSMethodTabID = Convert.ToInt32(this.comboBoxTransWay.SelectedValue.ToString(), 10);
                if (this.comboBoxTargetCountry.SelectedValue != null)
                    modelSJCKHTTabOld.MDCountryTabID = Convert.ToInt32(this.comboBoxTargetCountry.SelectedValue.ToString(), 10);
                //if (this.comboBoxContractNo.SelectedValue != null)
                //modelSJCKHTTabOld.PaidanRegisterTabID = modelSJCKHTTabOld.PaidanRegisterTabID;

                m_bllSJCKHTTab.TransUpdate(modelSJCKHTTabOld, m_SQLStringList, m_arrayList, 0);


                //修改相应的报关出口合同
                DataSet ds = m_bllBGCKHTTab.GetList("PaidanRegisterTabID = '" + modelSJCKHTTabOld.PaidanRegisterTabID + "'");
                int nBGCKHTTabID = 0;
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    nBGCKHTTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGCKHTTabID"].ToString(), 10); 
                }
                DGBY.Model.BGCKHTTab modelBGCKHTTab = m_bllBGCKHTTab.GetModel(nBGCKHTTabID);

                modelBGCKHTTab.JYCompDaiBiao = modelSJCKHTTabOld.JYCompDaiBiao;
                modelBGCKHTTab.SHCompDaiBiao = modelSJCKHTTabOld.SHCompDaiBiao;
                modelBGCKHTTab.DYZ = modelSJCKHTTabOld.DYZ;
                modelBGCKHTTab.BaoXian = modelSJCKHTTabOld.BaoXian;
                modelBGCKHTTab.FKCondition = modelSJCKHTTabOld.FKCondition;
                modelBGCKHTTab.SPCheck = modelSJCKHTTabOld.SPCheck;
                modelBGCKHTTab.ZYMethod = modelSJCKHTTabOld.ZYMethod;
                modelBGCKHTTab.SCPiNo = modelSJCKHTTabOld.SCPiNo;
                modelBGCKHTTab.QTThing = modelSJCKHTTabOld.QTThing;
                modelBGCKHTTab.Note = modelSJCKHTTabOld.Note;


                modelBGCKHTTab.QDDate = modelSJCKHTTabOld.QDDate;
                modelBGCKHTTab.JHDate = modelSJCKHTTabOld.JHDate;
                modelBGCKHTTab.isDisCount = this.checkBoxDisCountSJ.Checked;

                modelBGCKHTTab.InvoiceNo = modelSJCKHTTabOld.InvoiceNo;
                modelBGCKHTTab.InvoiceNote = modelSJCKHTTabOld.InvoiceNote;

                modelBGCKHTTab.QDPlaceTabID = modelSJCKHTTabOld.QDPlaceTabID;

                modelBGCKHTTab.YSMethodTabID = modelSJCKHTTabOld.YSMethodTabID;

                modelBGCKHTTab.MDCountryTabID = modelSJCKHTTabOld.MDCountryTabID;

                modelBGCKHTTab.JNHYPosTabID = 1;



                modelBGCKHTTab.ZYHarborTabID = 1;

                modelBGCKHTTab.SHCompTabID = 1;

                modelBGCKHTTab.BZSortTabID = 1;


                m_bllBGCKHTTab.TransUpdate(modelBGCKHTTab, m_SQLStringList, m_arrayList, 0);

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);

                if (bModifyShouldRecvMoney)
                {
                    DGBY.Model.FeeSJTruePayTab modelSJTruePayTabOld = m_bllFeeSJTruePayTab.GetModelByPaidanID(modelSJCKHTTabOld.PaidanRegisterTabID);
                    modelSJTruePayTabOld.ShouldRecvMoney = 20;
                    m_bllFeeSJTruePayTab.Update(modelSJTruePayTabOld);
                }

                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxContractNo.Text);
                ShowContractSheetByContractCode(this.comboBoxContractNo.Text);
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDelFillBoxSheet_Click(object sender, EventArgs e)
        {
            if (CheckSBDate())
            {
                MessageBox.Show("综合登记表该合同号已经登记申报日期，不能修改单据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DGBY.Model.SJCKHTTab modelSJCKHTTabOld = m_bllSJCKHTTab.GetModel(m_nSelectSJCKHTTabID);
            if (modelSJCKHTTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DGBY.Model.PaidanRegisterTab modelPaidanRegisterTabOld = m_bllPaidanRegisterTab.GetModel(modelSJCKHTTabOld.PaidanRegisterTabID);


            if (Frm_Main.m_nLoginUserId != modelPaidanRegisterTabOld.UserPasswordTabID)
            {
                MessageBox.Show("该合同号不是你派的单，不能删除!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllSJCKHTTab.Delete(m_nSelectSJCKHTTabID);

                DGBY.Model.LogRecTab modelLogRecTab = new DGBY.Model.LogRecTab();
                modelLogRecTab.LogDate = System.DateTime.Now.Date;
                modelLogRecTab.LogContent = Frm_Main.m_strLoginUser + "删除了合同号"
                    + modelPaidanRegisterTabOld.ContractCode + "的商检出口合同";
                m_bllLogRecTab.Add(modelLogRecTab);

                Frm_Main.Log2DB(this.Text, "删除", modelPaidanRegisterTabOld.ContractCode);
                ShowAllRecordInDataGridView("PaidanRegisterTab.PaidanRegisterTabID = '" + m_nSelectSJCKHTTabID.ToString() + "'");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.textBoxDYZ.Text = "";
                this.textBoxProductNo.Text = "";
                this.textBoxNote.Text = "";
                
                return;
            }
        }

         private void buttonCopySheet_Click(object sender, EventArgs e)
        {
            //DataSet ds = m_bllSJCKHTTab.ShowOneOutContractRecEx("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    MessageBox.Show("该合同编号已有对应的商检_出口合同了，因此无法复制单据，请重新选择合同编号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}

            //ds = m_bllSJCKHTTab.ShowOneOutContractRec("SJCKHTTab.SJCKHTDJNo = '" + this.textBoxSecondSheetNo.Text + "'");

            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    ds = m_bllSJPackingTab.ShowOneSJPackRec("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //    {
            //        int nPaidanRegisterTabID = 0;
            //        DateTime dt = System.DateTime.Now.Date;
            //        Int64 n64NewDJNo = MakeSJCKouDJNo();
            //        DataSet dsTemp = m_bllPaidanRegisterTab.GetAllPaiDanList("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            //        if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
            //        {
            //            nPaidanRegisterTabID = Convert.ToInt32(dsTemp.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
            //            m_bllSJCKHTTab.CopyDJNo(nPaidanRegisterTabID, dt, n64NewDJNo, Convert.ToInt64(this.textBoxSecondSheetNo.Text, 10));
            //            ShowContractSheetByContractCode(this.comboBoxContractNo.Text);
            //        }
            //    }
            //    else
            //    {
            //        MessageBox.Show("该单据号没有对应的商检_装箱单记录，因此无法复制单据，请重新输入单据号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("该单据号没有对应的商检_出口合同记录，因此无法复制单据，请重新输入单据号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
        }
        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        private void Contract2Excel(ArrayList lstExcelFileName, int nRows)
        {
            if (!Directory.Exists(m_strFolder))//判断是否存在
            {
                Directory.CreateDirectory(m_strFolder);//创建新路径
            }
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            DataTable dt = ds.Tables[0];

            //if (Frm_Main.m_bCompleteVer == true)
            {
                dt.Columns.Remove("单价");
                dt.Columns.Remove("金额");
                //dt.Columns["打折单价"].ColumnName = "报关单价";
                //dt.Columns["打折金额"].ColumnName = "报关金额";
            }
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}


            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");

            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            string strPort = dt1.Rows[0]["装运口岸"].ToString();
            m_ds = m_bllZYHarborTab.GetList("ZYHarborName = '" + strPort + "'");
            DataTable dtPort = m_ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条出口合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("出口合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string outFile = m_strFolder + "出口合同" + ".xls";
                    string templetFile = templetFilePath + "商检_出口合同.xlt";
                    lstExcelFileName.Add(outFile);

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckConstract_DataTableToExcel(dt, dt1
                            , DsBYCompInfo.Tables[0], DsSHCompInfo.Tables[0], nRows
                            , 16, 1, "页", 1, dtBizhi, dtPort);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("出口合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonExportToExcelContract(bool bIsDiscount)
        {

            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            DataTable dt = ds.Tables[0];
            if (bIsDiscount)
            {
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    //dt.Columns["打折单价"].ColumnName = "单价";
                    //dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");

            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            string strPort = dt1.Rows[0]["装运口岸"].ToString();
            m_ds = m_bllZYHarborTab.GetList("ZYHarborName = '" + strPort + "'");
            DataTable dtPort = m_ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条出口合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("出口合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "商检_出口合同.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckConstract_DataTableToExcel(dt, dt1
                            , DsBYCompInfo.Tables[0], DsSHCompInfo.Tables[0], nRows
                            , 16, 1, "页", 1, dtBizhi, dtPort);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("出口合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            bool bIsbIsDisCount = true;
            if (this.textBoxSJGuiCount.Text == "0")
            {
                MessageBox.Show("请设置商检柜数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
            //{
            //    bIsbIsDisCount = true;
            //}
            buttonExportToExcelContract(bIsbIsDisCount);
        }
        private void buttonPrintPreviewContract(bool bIsDiscount)
        {
            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            DataTable dt = ds.Tables[0];
            if (bIsDiscount)
            {
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                   // dt.Columns["打折单价"].ColumnName = "单价";
                   // dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            string strPort = dt1.Rows[0]["装运口岸"].ToString();
            m_ds = m_bllZYHarborTab.GetList("ZYHarborName = '" + strPort + "'");
            DataTable dtPort = m_ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检_出口合同.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);
                    //ExcelBase eB = new ExcelBase(templetFile, outFile);
                    try
                    {
                        eB.CommerCheckConstract_DataTableToExcelAndPrintPreview(dt, dt1
                            , DsBYCompInfo.Tables[0], DsSHCompInfo.Tables[0], nRows
                            , 16, 1, "页", 1, dtBizhi, dtPort);
                    }
                    catch (System.Exception ex)
                    {

                    }


                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonPrintPreview_Click(object sender, EventArgs e)
        {
                       
            if (this.textBoxSJGuiCount.Text == "0")
            {
                MessageBox.Show("请设置商检柜数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bool bIsbIsDisCount = true;
            buttonPrintPreviewContract(bIsbIsDisCount);
        }
        private void buttonPrintContract(bool bIsDiscount)
        {
            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            DataTable dt = ds.Tables[0];
                        
            if (bIsDiscount)
            {
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                   // dt.Columns["打折单价"].ColumnName = "单价";
                   // dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            string strPort = dt1.Rows[0]["装运口岸"].ToString();
            m_ds = m_bllZYHarborTab.GetList("ZYHarborName = '" + strPort + "'");
            DataTable dtPort = m_ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检_出口合同.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckConstract_DataTableToExcelAndPrint(dt, dt1
                            , DsBYCompInfo.Tables[0], DsSHCompInfo.Tables[0], nRows
                            , 16, 1, "页", 1, dtBizhi, dtPort);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonPrint_Click(object sender, EventArgs e)
        {
            if (this.textBoxSJGuiCount.Text == "0")
            {
                MessageBox.Show("请设置商检柜数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bool bIsbIsDisCount = true;
            buttonPrintContract(bIsbIsDisCount);
        }

        private void Frm_CommerCheckOutPutContract_FormClosing(object sender, FormClosingEventArgs e)
        {
            //eB.Close();
        }

        private void dateTimePickerMakeProtocolDate_ValueChanged(object sender, EventArgs e)
        {
            //this.dateTimePickerDeadLine.Value = this.dateTimePickerMakeProtocolDate.Value.AddMonths(1);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);

            InitColumnWidth1();
        }

        private void InitColumnWidth1()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["单据号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同编号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["卖方"].Width = 160;
            ultraGrid1.DisplayLayout.Bands[0].Columns["卖方代表人"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["卖方代表人英文名"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["买方"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["买方代表人"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["签订日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["签订地点"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["交货期限"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["包装"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["短溢装"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["商检柜数"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["运输方式"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["装运口岸"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["目的国家"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["保险"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["付款条件"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["商品检验"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["争议解决"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["生产批号"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["其它事项"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["发票号码"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["起运国"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否为鞋的装箱单"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["牌子"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂简称"].Width = 60;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["是否打折"].Width = 40;
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strSJCKHTTab = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectSJCKHTTabID = Convert.ToInt32(strSJCKHTTab, 10);
            cellSJCKHTTabClick();
        }

        private void ShowAllMXRecordInDataGridView(string strContractCode)
        {

            m_ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + strContractCode + "'");



            ultraGrid2.DataSource = null;

            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                //Decimal nNumTotal = 0;
                //Decimal dtJinETotal = 0;
                //Decimal dtMaoWeight = 0;
                //Decimal dtFactWeight = 0;
                //int nNumPiece = 0;

                //for (int i = 0; i < m_ds.Tables[0].Rows.Count; i++)
                //{
                //    nNumPiece += Convert.ToInt32(m_ds.Tables[0].Rows[i]["件数"].ToString(), 10);
                //    nNumTotal += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["数量"].ToString());
                //    dtJinETotal += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["金额"].ToString());
                //    dtMaoWeight += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["毛重"].ToString());
                //    dtFactWeight += Convert.ToDecimal(m_ds.Tables[0].Rows[i]["净重"].ToString());

                //}

                //DataRow dr = m_ds.Tables[0].NewRow();
                //dr["件数"] = nNumPiece;
                //dr["数量"] = nNumTotal;
                //dr["金额"] = dtJinETotal;
                //dr["毛重"] = dtMaoWeight;
                //dr["净重"] = dtFactWeight;
                //dr["商品名称"] = "合计";
                //m_ds.Tables[0].Rows.Add(dr.ItemArray);
                m_ds.Tables[0].Columns["打折单价"].ColumnName = "商检单价";
                m_ds.Tables[0].Columns["打折金额"].ColumnName = "商检金额";
                ultraGrid2.DataSource = m_ds.Tables[0];

                if (Frm_Main.m_bCompleteVer == false)
                {
                    ultraGrid2.DisplayLayout.Bands[0].Columns["单价"].Hidden = true;
                    ultraGrid2.DisplayLayout.Bands[0].Columns["金额"].Hidden = true;
                }
                //ultraGrid2.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                
            //    this.textBox1ContractNoGoodsDetail.Text = strContractCode;

            }
        }
        private void ultraGrid2_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid2.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strTemp = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[1].Text;
            if (strTemp == "合计")
                return;
            //string strSJPackGoodsMXTabID = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[0].Text;
            m_RowIndex = this.ultraGrid2.ActiveCell.Row.Index;
            //ShowGoodsDetail(m_RowIndex);
        }

        //private void ShowGoodsDetail(int RowIndex)
        //{
        //    int i = 1;
        //    this.textBoxGoodsName.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        //    this.textBoxMerial.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        //    this.textBoxGuiGe.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        //    this.textBoxNum.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        //    this.textBoxUnit.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        //    this.textBoxSingleCost.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        //    this.textBoxFOB.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        //    this.textBoxCoinUnit.Text = this.ultraGrid2.Rows[RowIndex].Cells[i++].Text;
        //}
        //private void ultraGrid2_AfterCellActivate(object sender, EventArgs e)
        //{
        //    int iIndex = this.ultraGrid2.ActiveCell.Row.Index;
        //    if (iIndex < 0)
        //        return;
        //    string strTemp = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[1].Text;
        //    if (strTemp == "合计")
        //        return;
        //    string strSJPackGoodsMXTabID = this.ultraGrid2.Rows[this.ultraGrid2.ActiveCell.Row.Index].Cells[0].Text;
        //    m_nSelectSJPackGoodsMXTabID = Convert.ToInt32(strSJPackGoodsMXTabID, 10);
        //    cellSJPackGoodsMXTabClick();

        //}

        private void ultraGrid2_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);

            InitColumnWidth2();
            foreach (UltraGridColumn column2 in ultraGrid2.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str != "金额" && str != "单价" && str != "商检单价" && str != "商检金额")
                {
                    column2.CellAppearance.BackColor = Color.Gray;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }

            List<string> lstTemp = new List<string>();
            lstTemp.Add("件数");
            lstTemp.Add("数量");
            lstTemp.Add("毛重");
            lstTemp.Add("净重");
            lstTemp.Add("金额");

            //if (Frm_Main.m_bCompleteVer == true)
            {
                lstTemp.Add("商检金额");
            }
 
      
            ClassUltraGridCommonSet.SetSumColumn(e, lstTemp);
        }

        private void InitColumnWidth2()
        {
            ultraGrid2.DisplayLayout.Bands[0].Columns["序号"].Width = 30;
            ultraGrid2.DisplayLayout.Bands[0].Columns["商品名称"].Width = 120;
            ultraGrid2.DisplayLayout.Bands[0].Columns["质地"].Width = 120;
            ultraGrid2.DisplayLayout.Bands[0].Columns["规格型号"].Width = 100;
            ultraGrid2.DisplayLayout.Bands[0].Columns["数量"].Width = 70;
            ultraGrid2.DisplayLayout.Bands[0].Columns["单位"].Width = 40;
            ultraGrid2.DisplayLayout.Bands[0].Columns["单价"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["金额"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["商检单价"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["商检金额"].Width = 80;
            ultraGrid2.DisplayLayout.Bands[0].Columns["币制"].Width = 40;
            ultraGrid2.DisplayLayout.Bands[0].Columns["件数"].Width = 40;
            ultraGrid2.DisplayLayout.Bands[0].Columns["毛重"].Width = 70;
            ultraGrid2.DisplayLayout.Bands[0].Columns["净重"].Width = 70;
        }

        private void Invoice2Excel(ArrayList lstExcelFileName)
        {
            if (!Directory.Exists(m_strFolder))//判断是否存在
            {
                Directory.CreateDirectory(m_strFolder);//创建新路径
            }
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

            }
            else
            {
                MessageBox.Show("商品明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = (DataTable)ds.Tables[0];


            //if (Frm_Main.m_bCompleteVer == true)
            {
                dt.Columns.Remove("单价");
                dt.Columns.Remove("金额");
                dt.Columns["打折单价"].ColumnName = "单价";
                dt.Columns["打折金额"].ColumnName = "金额";
            }
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}

            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;

            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];

            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];

            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条发票记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("发票记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string outFile = m_strFolder + "发票" + ".xls";
                    string templetFile = templetFilePath + "商检报关_发票.xlt";
                    lstExcelFileName.Add(outFile);

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.Invoice_DataTableToExcel(dt, dt1, 20, 15, 1, "页", 1, dtComp, dtBizhi);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("货物明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        
        private void buttonInvoiceExcelClick(bool bIsDiscount)
        {
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

            }
            else
            {
                MessageBox.Show("商品明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = (DataTable)ds.Tables[0];
             
            if (bIsDiscount)
            {
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            else
            {
                dt.Columns.Remove("打折单价");
                dt.Columns.Remove("打折金额");
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;

            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];

            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];

            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条发票记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("发票记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "商检报关_发票.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.Invoice_DataTableToExcel(dt, dt1, 20, 15, 1, "页", 1, dtComp, dtBizhi);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("货物明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonInvoiceExcel_Click(object sender, EventArgs e)
        {
            if (this.textBoxSJGuiCount.Text == "0")
            {
                MessageBox.Show("请设置商检柜数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bool bIsbIsDisCount = true;
            buttonInvoiceExcelClick(bIsbIsDisCount);
        }
        private void buttonInvoicePrintPreviewClick(bool bIsDiscount)
        {
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

            }
            else
            {
                MessageBox.Show("商品明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = (DataTable)ds.Tables[0];
            if (bIsDiscount)
            {
               // if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            else
            {
                dt.Columns.Remove("打折单价");
                dt.Columns.Remove("打折金额");
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条发票记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("发票记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检报关_发票.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.Invoice_DataTableToExcelAndPrintPreview(dt, dt1, 20, 15, 1, "页", 1, dtComp, dtBizhi);
                    }
                    catch (System.Exception ex)
                    {

                    }


                }
            }
            else
            {
                MessageBox.Show("货物明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonInvoicePrintPreview_Click(object sender, EventArgs e)
        {
            if (this.textBoxSJGuiCount.Text == "0")
            {
                MessageBox.Show("请设置商检柜数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bool bIsbIsDisCount = true;
            buttonInvoicePrintPreviewClick(bIsbIsDisCount);
        }


        private void buttonInvoicePrintClick(bool bIsDiscount)
        {
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {

            }
            else
            {
                MessageBox.Show("商品明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = (DataTable)ds.Tables[0];
            if (bIsDiscount)
            {
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            else
            {
                dt.Columns.Remove("打折单价");
                dt.Columns.Remove("打折金额");
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条发票记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("发票记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检报关_发票.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.Invoice_DataTableToExcelAndPrint(dt, dt1, 20, 15, 1, "页", 1, dtComp, dtBizhi);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("货物明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonInvoicePrint_Click(object sender, EventArgs e)
        {
            if (this.textBoxSJGuiCount.Text == "0")
            {
                MessageBox.Show("请设置商检柜数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            bool bIsbIsDisCount = true;
            buttonInvoicePrintClick(bIsbIsDisCount);
        }

        private void Frm_CommerCheckOutPutContract_Move(object sender, EventArgs e)
        {
            try
            {
                Frm_Main.m_nContractSelectValue = (int)this.comboBoxContractNo.SelectedValue;
                InitContractNoCombox();
            }
            catch (System.Exception ex)
            {

            }

        }

        private void Frm_CommerCheckOutPutContract_Activated(object sender, EventArgs e)
        {

            InitContractNoCombox();
            SyncContractNo();
        }

        private void Frm_CommerCheckOutPutContract_Deactivate(object sender, EventArgs e)
        {
            try
            {
                Frm_Main.m_nContractSelectValue = (int)this.comboBoxContractNo.SelectedValue;

            }
            catch (System.Exception ex)
            {

            }
        }

        private void ZXD2Excel(ArrayList lstExcelFileName, int nRows)
        {
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMXForZxd("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];
            DataTable dt1 = (DataTable)ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string outFile = m_strFolder + "装箱单.xls";
                    string templetFile = templetFilePath + "商检_装箱单.xlt";
                    lstExcelFileName.Add(outFile);
                    ExcelBase eB = new ExcelBase(templetFile, outFile);
                    
                    try
                    {
                        eB.CommerCheckFillBox_DataTableToExcel(dt, dt1, nRows, 19, 1, "页", 1, dtComp);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonZxdExcel_Click(object sender, EventArgs e)
        {
            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMXForZxd("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];
            DataTable dt1 = (DataTable)ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "商检_装箱单.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckFillBox_DataTableToExcel(dt, dt1, nRows, 19, 1, "页", 1, dtComp);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonZxdPrintPrew_Click(object sender, EventArgs e)
        {
            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMXForZxd("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];
            DataTable dt1 = (DataTable)ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检_装箱单.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckFillBox_DataTableToExcelAndPrintPreview(dt, dt1, nRows, 19
                            , 1, "页", 1, dtComp);
                    }
                    catch (System.Exception ex)
                    {

                    }


                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonZxdPrint_Click(object sender, EventArgs e)
        {
            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMXForZxd("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];
            DataTable dt1 = (DataTable)ultraGrid1.DataSource;
            string strCompanyName = dt1.Rows[0]["卖方"].ToString();
            ds = bllBYCompanyTab.GetList("CompanyName = '" + strCompanyName + "'");
            DataTable dtComp = ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条装箱单记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("装箱单记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "商检_装箱单.xlt";
                    string outFile = "";
                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckFillBox_DataTableToExcelAndPrint(dt, dt1, nRows, 19, 1, "页", 1, dtComp);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("装箱单明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
       
        private void buttonModify_Click(object sender, EventArgs e)
        {

          //  ModifyDetail();
        }

        //private void textBoxSingleCost_TextChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        Frm_Main.WriteLog("textBoxSingleCost_TextChanged!");
        //        if (this.textBoxNum.Text != "" && this.textBoxSingleCost.Text != "" && !m_bFlag)
        //        {
        //            Frm_Main.WriteLog("textBoxSingleCost_TextChanged!222");
        //            m_bFlag = true;
        //            int nFPieceNum = Convert.ToInt32(this.textBoxNum.Text, 10);
        //            Decimal dTotalCost = Convert.ToDecimal(this.textBoxSingleCost.Text) * (Decimal)nFPieceNum;
        //            this.textBoxFOB.Text = dTotalCost.ToString();
        //            m_bFlag = false;
        //        }
        //    }
        //    catch (System.Exception ex)
        //    {
        //        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        //    }

        //}

        //private void textBoxFOB_TextChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        Frm_Main.WriteLog("textBoxFOB_TextChanged!");

        //        if (this.textBoxNum.Text != "" && this.textBoxFOB.Text != "" && !m_bFlag)
        //        {
        //            Frm_Main.WriteLog("textBoxFOB_TextChanged!222");
        //            m_bFlag = true;
        //            int nFPieceNum = Convert.ToInt32(this.textBoxNum.Text, 10);
        //            if (nFPieceNum > 0)
        //            {
        //                Decimal dSingleCost = Convert.ToDecimal(this.textBoxFOB.Text) / (Decimal)nFPieceNum;
        //                this.textBoxSingleCost.Text = dSingleCost.ToString();
        //            }
        //            m_bFlag = false;

        //        }
        //    }
        //    catch (System.Exception ex)
        //    {
        //        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

        //    }

        //}

        private void ModifyContractDetail()
        {

            DataSet ds = m_bllSJCKHTTab.GetAllSJPackingID(this.comboBoxContractNo.Text);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nPackingId = Convert.ToInt32(ds.Tables[0].Rows[i]["SJPackingTabID"].ToString(), 10);
                    
                    DataSet ds2 = m_bllSJCKHTTab.FindSJPackGoodsMXTabID("", nPackingId.ToString());
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            int nSJPackGoodsMXTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["SJPackGoodsMXTabID"].ToString(), 10);
                            Decimal nNum = Convert.ToDecimal(ds2.Tables[0].Rows[j]["Number"].ToString());
                            DGBY.Model.SJPackGoodsMXTab modelSJPackGoodsMXTabOld = m_bllSJPackGoodsMXTab.GetModel(nSJPackGoodsMXTabID);

                            if (checkBoxDisCountSJ.Checked)
                            {
                                modelSJPackGoodsMXTabOld.GouHHTDCost = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.CKHTDCost * Frm_Main.m_dtDiscountFactor, 2);
                                modelSJPackGoodsMXTabOld.GouHHTJinE = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.GouHHTDCost * nNum, 2);
                            }
                            else
                            {
                                modelSJPackGoodsMXTabOld.GouHHTDCost = modelSJPackGoodsMXTabOld.CKHTDCost;
                                modelSJPackGoodsMXTabOld.GouHHTJinE = modelSJPackGoodsMXTabOld.CKHTJinE;
                            }
                            m_bllSJPackGoodsMXTab.Update(modelSJPackGoodsMXTabOld);
                        }
                    }
                    DGBY.Model.SJPackingTab modelSJPackingTabOld = m_bllSJPackingTab.GetModel(nPackingId);

                    DataSet ds3 = m_bllBGPackingTab.GetList("BGSJPackDJNo = '" + modelSJPackingTabOld.SJPackDJNo.ToString() + "'");
                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        int nBGPackingId = Convert.ToInt32(ds3.Tables[0].Rows[0]["BGPackingTabID"].ToString(), 10);
                        ds2 = m_bllSJCKHTTab.FindBGPackGoodsMXTabID("", nBGPackingId.ToString());
                        if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                        {
                            for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                            {
                                int nBGPackGoodsMXTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["BGPackGoodsMXTabID"].ToString(), 10);
                                Decimal nNum = Convert.ToDecimal(ds2.Tables[0].Rows[j]["FNumber"].ToString());
                                DGBY.Model.BGPackGoodsMXTab modelBGPackGoodsMXTabOld = m_bllBGPackGoodsMXTab.GetModel(nBGPackGoodsMXTabID);

                                if (checkBoxDisCountSJ.Checked)
                                {
                                    modelBGPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(modelBGPackGoodsMXTabOld.CKHTDCost * Frm_Main.m_dtDiscountFactor, 2);
                                    modelBGPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelBGPackGoodsMXTabOld.HMaoWeight * nNum, 2);
                                }
                                else
                                {
                                    modelBGPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(modelBGPackGoodsMXTabOld.CKHTDCost, 2);
                                    modelBGPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelBGPackGoodsMXTabOld.CKHTJinE, 2);
                                }

                                m_bllBGPackGoodsMXTab.Update(modelBGPackGoodsMXTabOld);
                            }
                        }
                    }


                }
            }
        }
        private void ModifyDetail(int nIndex)
        {
            if (CheckSBDate())
            {
                MessageBox.Show("综合登记表该合同号已经登记申报日期，不能修改单据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            UltraGridRow CurrentUGR;
            CurrentUGR = ultraGrid2.Rows[nIndex];
            Decimal dtDisPrice = 0;
            if (Frm_Main.m_bCompleteVer == true)
            {
                dtDisPrice = Convert.ToDecimal(CurrentUGR.Cells["商检单价"].Value.ToString());
            }
   
            
            int nDanweiId = 0;
            DataSet ds = m_bllNumDanWeiTab.GetList("NumDanWeiName = '" + CurrentUGR.Cells["单位"].Value.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                nDanweiId = Convert.ToInt32(ds.Tables[0].Rows[0]["NumDanWeiTabID"].ToString(), 10);
            }

            ds = m_bllSJCKHTTab.GetAllSJPackingID(this.comboBoxContractNo.Text);
            bool bIsDisCount = false;
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nPackingId = Convert.ToInt32(ds.Tables[0].Rows[i]["SJPackingTabID"].ToString(), 10);
                    bIsDisCount = (bool)ds.Tables[0].Rows[i]["isDisCount"];
                    string strWhere = "GoodsMXName = '" + CurrentUGR.Cells["商品名称"].Value.ToString() + "'"
               + " and GGXHZDEtc = '" + CurrentUGR.Cells["质地"].Value.ToString() + "'"
               + " and GGXHZDEtc2 = '" + CurrentUGR.Cells["规格型号"].Value.ToString() + "'"
               + " and NumDanWeiTabID = '" + nDanweiId + "'";
                    DataSet ds2 = m_bllSJCKHTTab.FindSJPackGoodsMXTabID(strWhere, nPackingId.ToString());
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            int nSJPackGoodsMXTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["SJPackGoodsMXTabID"].ToString(), 10);
                            Decimal nNum = Convert.ToDecimal(ds2.Tables[0].Rows[j]["Number"].ToString());
                            DGBY.Model.SJPackGoodsMXTab modelSJPackGoodsMXTabOld = m_bllSJPackGoodsMXTab.GetModel(nSJPackGoodsMXTabID);
                            modelSJPackGoodsMXTabOld.CKHTDCost = Convert.ToDecimal(CurrentUGR.Cells["商检单价"].Value.ToString());
                            modelSJPackGoodsMXTabOld.CKHTJinE = CommonCompute.ChinaRound(Convert.ToDecimal(CurrentUGR.Cells["商检单价"].Value.ToString()) * nNum, 2); 
                            //modelSJPackGoodsMXTabOld.CKHTJinE = Convert.ToDecimal(CurrentUGR.Cells["金额"].Value.ToString());
                            if (bIsDisCount)
                            {
                                modelSJPackGoodsMXTabOld.GouHHTDCost = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.CKHTDCost * Frm_Main.m_dtDiscountFactor ,2);
                                modelSJPackGoodsMXTabOld.GouHHTJinE = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.GouHHTDCost * nNum, 2); 
                            }
                            else
                            {
                                modelSJPackGoodsMXTabOld.GouHHTDCost = modelSJPackGoodsMXTabOld.CKHTDCost;
                                modelSJPackGoodsMXTabOld.GouHHTJinE = modelSJPackGoodsMXTabOld.CKHTJinE;
                            }
                            if (Frm_Main.m_bCompleteVer == true)
                            {
                                if (dtDisPrice != modelSJPackGoodsMXTabOld.GouHHTDCost)
                                {
                                    modelSJPackGoodsMXTabOld.GouHHTDCost = CommonCompute.ChinaRound(dtDisPrice, 2);
                                    modelSJPackGoodsMXTabOld.GouHHTJinE = CommonCompute.ChinaRound(modelSJPackGoodsMXTabOld.GouHHTDCost * nNum, 2);
                                }
                            }
        
                            
                            m_bllSJPackGoodsMXTab.Update(modelSJPackGoodsMXTabOld);
                        }
                    }
                    DGBY.Model.SJPackingTab modelSJPackingTabOld = m_bllSJPackingTab.GetModel(nPackingId);

                    DataSet ds3 = m_bllBGPackingTab.GetList("BGSJPackDJNo = '" + modelSJPackingTabOld.SJPackDJNo.ToString() + "'");
                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        int nBGPackingId = Convert.ToInt32(ds3.Tables[0].Rows[0]["BGPackingTabID"].ToString(), 10);
                        ds2 = m_bllSJCKHTTab.FindBGPackGoodsMXTabID(strWhere, nBGPackingId.ToString());
                        if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                        {
                            for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                            {
                                int nBGPackGoodsMXTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["BGPackGoodsMXTabID"].ToString(), 10);
                                Decimal nNum = Convert.ToDecimal(ds2.Tables[0].Rows[j]["FNumber"].ToString());
                                DGBY.Model.BGPackGoodsMXTab modelBGPackGoodsMXTabOld = m_bllBGPackGoodsMXTab.GetModel(nBGPackGoodsMXTabID);
                                modelBGPackGoodsMXTabOld.CKHTDCost = Convert.ToDecimal(CurrentUGR.Cells["商检单价"].Value.ToString());
                                modelBGPackGoodsMXTabOld.CKHTJinE = CommonCompute.ChinaRound(Convert.ToDecimal(CurrentUGR.Cells["商检单价"].Value.ToString()) * nNum, 2); 
                                //modelBGPackGoodsMXTabOld.CKHTJinE = Convert.ToDecimal(CurrentUGR.Cells["金额"].Value.ToString());
                                if (bIsDisCount)
                                {
                                    modelBGPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(modelBGPackGoodsMXTabOld.CKHTDCost * Frm_Main.m_dtDiscountFactor, 2);
                                    modelBGPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelBGPackGoodsMXTabOld.HMaoWeight * nNum,2);
                                }
                                else
                                {
                                    //modelBGPackGoodsMXTabOld.HMaoWeight = CommonCompute.ChinaRound(modelBGPackGoodsMXTabOld.CKHTDCost, 2);
                                    //modelBGPackGoodsMXTabOld.HZMaoWeight = CommonCompute.ChinaRound(modelBGPackGoodsMXTabOld.CKHTJinE, 2);
                                    modelBGPackGoodsMXTabOld.HMaoWeight = modelBGPackGoodsMXTabOld.CKHTDCost;
                                    modelBGPackGoodsMXTabOld.HZMaoWeight = modelBGPackGoodsMXTabOld.CKHTJinE;
                                }

                                m_bllBGPackGoodsMXTab.Update(modelBGPackGoodsMXTabOld);
                            }
                        }
                    }


                }
            }

            ShowAllMXRecordInDataGridView(this.comboBoxContractNo.Text);
        }
        private void ultraGrid2_BeforeRowUpdate(object sender, Infragistics.Win.UltraWinGrid.CancelableRowEventArgs e)
        {
            ModifyDetail(e.Row.Index);
            m_bHaveChange = false;
        }

        private void ultraGrid2_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid2.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid2.ActiveCell.Column.Index;
                        int nIndex = ultraGrid2.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid2.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid2.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid2.ActiveCell.Column.Index;
                        int nIndex = ultraGrid2.ActiveRow.Index;
                        if (nIndex < ultraGrid2.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid2.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid2.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid2.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private void ultraGrid2_AfterCellUpdate(object sender, CellEventArgs e)
        {
            if (Frm_Main.m_bCompleteVer == true)
            {
                if (e.Cell.Column.Key == "金额")
                {
                    if (m_bHaveChange)
                        return;
                    int nIndex = e.Cell.Row.Index;
                    UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
                    Decimal dJinE = (Decimal)e.Cell.Value;
                    Decimal nFPieceNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                    if (nFPieceNum > 0)
                    {
                        m_bHaveChange = true;
                        decimal dtTemp = dJinE / nFPieceNum;
                        CurrentUGR.Cells["单价"].Value = dtTemp;
                     

                    }
                }
                if (e.Cell.Column.Key == "单价")
                {
                    int nIndex = e.Cell.Row.Index;
                    UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
                    Decimal dSingleCost = (Decimal)e.Cell.Value;
                    Decimal nFPieceNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                    if (!m_bHaveChange)
                    {
                     
               
                        if (nFPieceNum > 0)
                        {
                            CurrentUGR.Cells["金额"].Value = CommonCompute.ChinaRound(dSingleCost * nFPieceNum, 2);

                        }
                    }

                    DataSet ds = m_bllSJCKHTTab.GetAllSJPackingID(this.comboBoxContractNo.Text);
                    bool bIsDisCount = false;
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        //if (Frm_Main.m_bCompleteVer == true)
                        {
                            bIsDisCount = (bool)ds.Tables[0].Rows[0]["isDisCount"];
                            if (bIsDisCount)
                            {
                                CurrentUGR.Cells["商检单价"].Value = CommonCompute.ChinaRound(dSingleCost * Frm_Main.m_dtDiscountFactor, 4);
                                CurrentUGR.Cells["商检金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost * Frm_Main.m_dtDiscountFactor, 2) * nFPieceNum, 2);

                            }
                            else
                            {
                                //CurrentUGR.Cells["商检单价"].Value = CommonCompute.ChinaRound(dSingleCost, 4);
                                //CurrentUGR.Cells["商检金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost, 2) * nFPieceNum, 2);
                                CurrentUGR.Cells["商检单价"].Value = dSingleCost;
                                CurrentUGR.Cells["商检金额"].Value = CommonCompute.ChinaRound(dSingleCost * nFPieceNum, 2);

                            }
                        }

                    }
                }
            }
            else
            {
                if (e.Cell.Column.Key == "商检金额")
                {
                    if (m_bHaveChange)
                        return;
                    int nIndex = e.Cell.Row.Index;
                    UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
                    Decimal dJinE = (Decimal)e.Cell.Value;
                    Decimal nFPieceNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                    if (nFPieceNum > 0)
                    {
                        m_bHaveChange = true;
                        decimal dtTemp = dJinE / nFPieceNum;
                        CurrentUGR.Cells["商检单价"].Value = dtTemp;
                        //CurrentUGR.Cells["商检单价"].Value = CommonCompute.ChinaRound(dJinE / nFPieceNum, 2);

                    }
                }
                if (e.Cell.Column.Key == "商检单价")
                {
                    if (m_bHaveChange)
                        return;
                    int nIndex = e.Cell.Row.Index;
                    UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
                    Decimal dSingleCost = (Decimal)e.Cell.Value;
                    Decimal nFPieceNum = Convert.ToDecimal(CurrentUGR.Cells["数量"].Value.ToString());
                    if (nFPieceNum > 0)
                    {
                      
                        //CurrentUGR.Cells["商检金额"].Value = CommonCompute.ChinaRound(dSingleCost * nFPieceNum, 2);
                        CurrentUGR.Cells["商检金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost, 2) * nFPieceNum, 2);

                    }

                    //DataSet ds = m_bllSJCKHTTab.GetAllSJPackingID(this.comboBoxContractNo.Text);
                    //bool bIsDisCount = false;
                    //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    //{
                    //    //if (Frm_Main.m_bCompleteVer == true)
                    //    {
                    //        bIsDisCount = (bool)ds.Tables[0].Rows[0]["isDisCount"];
                    //        if (bIsDisCount)
                    //        {
                    //            if (!m_bHaveChange)
                    //            {
                    //                m_bHaveChange = true;
                    //                CurrentUGR.Cells["商检单价"].Value = CommonCompute.ChinaRound(dSingleCost * Frm_Main.m_dtDiscountFactor, 2);
                    //            }
                                
                    //            CurrentUGR.Cells["商检金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost * Frm_Main.m_dtDiscountFactor, 2) * nFPieceNum, 2);
                    //        }
                    //        else
                    //        {
                    //            if (!m_bHaveChange)
                    //            {
                    //                m_bHaveChange = true;
                    //                CurrentUGR.Cells["商检单价"].Value = CommonCompute.ChinaRound(dSingleCost, 2);
                    //            }
                    //            CurrentUGR.Cells["商检金额"].Value = CommonCompute.ChinaRound(CommonCompute.ChinaRound(dSingleCost, 2) * nFPieceNum, 2);
                    //        }
                   
                    //    }

                    //}

                }
            }
        }

        private void buttonTogether_Click(object sender, EventArgs e)
        {

        }

        private void textBoxSplit_TextChanged(object sender, EventArgs e)
        {

            //if(this.textBoxSplit.Text != "")
            //{
            //    int n = Convert.ToInt32(this.textBoxSplit.Text, 10);
            //    if(n>20 || n < 10)
            //    {
            //        MessageBox.Show("必须为10到20之间的一个数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    }
            //}
        }
        private void ExportXiaDanDetail(bool bIsDisCount)
        {
            //if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
            //{
            //    MessageBox.Show("Pressed " + Keys.Shift);
            //}
            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            //int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            DataSet ds = m_bllSJCKHTTab.ShowAllCKHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");

            DataTable dt = ds.Tables[0];

            if (bIsDisCount)
            {
                //if (Frm_Main.m_bCompleteVer == true)
                {
                    dt.Columns.Remove("单价");
                    dt.Columns.Remove("金额");
                    dt.Columns["打折单价"].ColumnName = "单价";
                    dt.Columns["打折金额"].ColumnName = "金额";
                }
                //else
                //{
                //    dt.Columns.Remove("打折单价");
                //    dt.Columns.Remove("打折金额");
                //}
            }
            else
            {
                dt.Columns.Remove("打折单价");
                dt.Columns.Remove("打折金额");
            }
            DataTable dt1 = (DataTable)this.ultraGrid1.DataSource;
            DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");

            string strBizhi = dt.Rows[0]["币制"].ToString();
            ds = m_bllBiZhiTab.GetList("BiZhiName = '" + strBizhi + "'");
            DataTable dtBizhi = ds.Tables[0];
            string strPort = dt1.Rows[0]["装运口岸"].ToString();
            m_ds = m_bllZYHarborTab.GetList("ZYHarborName = '" + strPort + "'");
            DataTable dtPort = m_ds.Tables[0];
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条出口合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("出口合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "香港order sheet.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.HongKongOrderSheet_DataTableToExcel(dt, dt1
                            , DsBYCompInfo.Tables[0], DsSHCompInfo.Tables[0], 20
                            , 13, 1, "页", 1, dtBizhi, dtPort);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("出口合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bool bIsbIsDisCount = true;
            //if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
            //{
            //    bIsbIsDisCount = true;
            //}
            ExportXiaDanDetail(bIsbIsDisCount);
        }

        private void buttonZxdExcel_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void buttonZxdExcel_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void ultraGrid2_CellChange(object sender, CellEventArgs e)
        {
         
        }
        private void SJGouContract2Excel(ArrayList lstExcelFileName)
        {
            if (Directory.Exists(m_strFolder))//判断是否存在
            {
                //if (File.Exists(strFolder + @"\" + strFin + ".jpg"))
                //    return;

            }
            else
            {
                //Response.Write("不存在，正在创建");
                Directory.CreateDirectory(m_strFolder);//创建新路径
            }

            DataSet ds = m_bllSJGouHHTTab.ShowAllGouHHTMX("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt = ds.Tables[0];

            //   if (Frm_Main.m_bCompleteVer == true)
            {
                dt.Columns.Remove("单价");
                dt.Columns.Remove("金额");
                //dt.Columns["打折单价"].ColumnName = "单价";
                //dt.Columns["打折金额"].ColumnName = "金额";
            }
            //else
            //{
            //    dt.Columns.Remove("打折单价");
            //    dt.Columns.Remove("打折金额");
            //}
            DataSet dsTemp = m_bllSJGouHHTTab.ShowOneBuyGoodsRecEx("ContractCode = '" + this.comboBoxContractNo.Text + "'");
            DataTable dt1 = dsTemp.Tables[0];
            //DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            //DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            if (dt1 != null)
            {
                if (dt1.Rows.Count > 1)
                {
                    MessageBox.Show("请选择不多于一条购货合同记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("购货合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {

                    string outFile = m_strFolder + "购货合同" + ".xls";
                    string templetFile = templetFilePath + "商检_购货合同草稿.xlt";
                    lstExcelFileName.Add(outFile);

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.CommerCheckBuyGoodsConstract_DataTableToExcel(dt, dt1, 20, 8, 1, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("购货合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void buttonTogether_Click_1(object sender, EventArgs e)
        {
            if (this.textBoxSJGuiCount.Text == "0")
            {
                MessageBox.Show("请设置商检柜数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (ClassInputValid.IsValidSplitLine(this.errorProvider1, this.textBoxSplit))
                return;
            int nRows = Convert.ToInt32(this.textBoxSplit.Text, 10);
            ArrayList lstExcelFileName = new ArrayList();
            if (this.textBoxPort.Text == "东莞" || this.textBoxPort.Text == "深圳")
            {
                
                SJGouContract2Excel(lstExcelFileName);
            }
            Invoice2Excel(lstExcelFileName);

            ZXD2Excel(lstExcelFileName, nRows);
            Contract2Excel(lstExcelFileName, nRows);

            ExcelBase eB = new ExcelBase();
            string outFile = m_strFolder + this.comboBoxContractNo.Text + "商检.xls";
            eB.Books2Sheets(outFile, lstExcelFileName, true);
            eB.CloseAll();
        }

        private void ultraGrid2_AfterColPosChanged(object sender, AfterColPosChangedEventArgs e)
        {

        }

        //private void textBoxSingleCost_TextChanged(object sender, EventArgs e)
        //{

        //}

        //private void textBoxFOB_TextChanged(object sender, EventArgs e)
        //{

        //}
    }
}